package com.huaao.micropolice.root.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.MDC;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.huaao.micropolice.core.common.Constants;
import com.huaao.micropolice.root.commons.CacheUtils;

public class MenuIdInterceptor implements HandlerInterceptor{
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String uri = request.getRequestURI();
		if(!StringUtils.isBlank(uri) && uri.lastIndexOf("/") != -1){
			String menuId = (String) CacheUtils.get(Constants.CACHE_MENU_URL, uri.substring(0, uri.lastIndexOf("/")));
			if(menuId != null){
				MDC.put(Constants.MENU_ID, menuId);
			}
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

}
